package cn.edu.njust.alg.leetcode;

import java.util.HashMap;

/**
 * 创建时间：2015年6月15日 上午11:47:37 项目名称：AlgorithmExercise
 * 
 * @author Zhou Xiumin
 * @version 1.0
 * @since JDK 1.7.0_21 文件名称：Leetcode_0_t.java 类说明：
 */
public class Leetcode_205_Isomorphic_Strings {

	public static boolean isIsomorphic(String s, String t) {
		HashMap<Character, Character> map = new HashMap<Character, Character>();
		if (s.length() != t.length()) {
			return false;
		}
		int len = s.length();
		if (len == 0) {
			return true;
		}
		for (int i = 0; i < len; i++) {
			char cs = s.charAt(i);
			char ct = t.charAt(i);
			Character p = map.get(cs);
			if (p == null) {
				map.put(cs, ct);
			} else if (p != ct) {
				return false;
			}
		}
		map.clear();
		for (int i = 0; i < len; i++) {
			char cs = s.charAt(i);
			char ct = t.charAt(i);
			Character p = map.get(ct);
			if (p == null) {
				map.put(ct, cs);
			} else if (p != cs) {
				return false;
			}
		}
		return true;
	}

	public static void main(String[] args) {

		System.out.println(isIsomorphic("ab", "aa"));
		System.out.println(isIsomorphic("foo", "bar"));
		System.out.println(isIsomorphic("papar", "title"));

	}

}
